using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Globalization;

namespace VSSDKAssist.Assets.Converters
{
	/// <summary>
	/// TypeConverter for a command id
	/// </summary>
	public class CommandIdConverter : StringConverter
	{
		/// <summary>
		/// Converts the specified value object to a <see cref="T:System.String"></see> object.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
		/// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"></see> to use.</param>
		/// <param name="value">The <see cref="T:System.Object"></see> to convert.</param>
		/// <returns>
		/// An <see cref="T:System.Object"></see> that represents the converted value.
		/// </returns>
		/// <exception cref="T:System.NotSupportedException">The conversion could not be performed. </exception>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int16.Parse(System.String,System.Globalization.NumberStyles)"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			string number = value as string;

			if(number != null)
			{
				try
				{
					return Int16.Parse(number, NumberStyles.Integer);
				}
				catch
				{
					return null;
				}
			}

			return base.ConvertFrom(context, culture, value);
		}
	}
}